#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>

#include "cxglobal.h"
#include "CxSystemBuilder.h"
#include "cxdbsetdialog.h"
#include "cxstartupwizard.h"
#include <opencv2/opencv.hpp>

#include <QMessageBox>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    InitWindow();
}

void    MainWindow::InitWindow()
{
    //Set imgFrame names
    ui->frame->SetLableText("Channal 1");
    ui->frame_2->SetLableText("Channal 2");
    ui->frame_3->SetLableText("Channal 3");
    ui->frame_4->SetLableText("Channal 4");

    //Add toolbar
    ui->mainToolBar->addAction(ui->actionSaveLog);
    ui->mainToolBar->addAction(ui->actionLoadSet);
    ui->mainToolBar->addAction(ui->actionServer);
    ui->mainToolBar->addAction(ui->actionSet);
    ui->mainToolBar->addAction(ui->actionPhone);


    connect(this,SIGNAL(sig_showImage_c1(const cv::Mat&)),ui->frame,SIGNAL(sig_showImage(const cv::Mat&)),Qt::QueuedConnection);
    connect(this,SIGNAL(sig_showImage_c2(const cv::Mat&)),ui->frame_2,SIGNAL(sig_showImage(const cv::Mat&)),Qt::QueuedConnection);
    connect(this,SIGNAL(sig_showImage_c3(const cv::Mat&)),ui->frame_3,SIGNAL(sig_showImage(const cv::Mat&)),Qt::QueuedConnection);
    connect(this,SIGNAL(sig_showImage_c4(const cv::Mat&)),ui->frame_4,SIGNAL(sig_showImage(const cv::Mat&)),Qt::QueuedConnection);


    connect(this,SIGNAL(sig_showCarcount_c1(int,int)),ui->frame,SIGNAL(sig_showCarcount(int,int)),Qt::QueuedConnection);
    connect(this,SIGNAL(sig_showCarcount_c2(int,int)),ui->frame_2,SIGNAL(sig_showCarcount(int,int)),Qt::QueuedConnection);
    connect(this,SIGNAL(sig_showCarcount_c3(int,int)),ui->frame_3,SIGNAL(sig_showCarcount(int,int)),Qt::QueuedConnection);
    connect(this,SIGNAL(sig_showCarcount_c4(int,int)),ui->frame_4,SIGNAL(sig_showCarcount(int,int)),Qt::QueuedConnection);

    connect(this,SIGNAL(sig_updateAll()),ui->controlFrame,SIGNAL(sig_updateAll()));
}


MainWindow::~MainWindow()
{
    delete ui;
}

//Startup wizard
void MainWindow::on_actionWizard_triggered()
{
    CxStartupWizard wz(this);
    connect(&wz,SIGNAL(sig_updateAll()),this,SIGNAL(sig_updateAll()));
    wz.exec();
    disconnect(&wz,SIGNAL(sig_updateAll()),this,SIGNAL(sig_updateAll()));
}



 void        MainWindow::showImage(int  n, const cv::Mat& img)
 {
     switch(n)
     {
         case 0:
             emit sig_showImage_c1(img);
             break;
         case 1:
             emit sig_showImage_c2(img);
             break;
         case 2:
             emit sig_showImage_c3(img);
             break;
         case 3:
             emit sig_showImage_c4(img);
             break;
        default:
             break;
     }
 }


 void        MainWindow::showCarcount(int  n,int in,int  out)
 {
     switch(n)
     {
         case 0:
             emit sig_showCarcount_c1(in,out);
             break;
         case 1:
             emit sig_showCarcount_c2(in,out);
             break;
         case 2:
             emit sig_showCarcount_c3(in,out);
             break;
         case 3:
             emit sig_showCarcount_c4(in,out);
             break;
        default:
             break;
     }
 }



//Exit signal
void MainWindow::on_actionExit_triggered()
{
    QApplication::instance()->exit();
}

//Load set file
void MainWindow::on_actionLoadSet_triggered()
{
    QMessageBox::warning(this,"Warning","Load complete..");
}

//Save set file
void MainWindow::on_actionSaveSet_triggered()
{
    QMessageBox::warning(this,"Warning","Save complete ...");
}


//Save log file
void MainWindow::on_actionSaveLog_triggered()
{
    CxGlobal gl;

    for(int i=0;i<4;++i)
    {
        int in,out;
        ui->frame->GetCarCount(in,out);
        gl.writeSetfile(i,in,out);
    }


    QMessageBox::warning(this,"Warning","Save log file compete");
}

void MainWindow::on_actionServer_triggered()
{
    CxDbSetDialog dlg;
    dlg.exec();

}

void MainWindow::on_action32_triggered()
{
    CxGlobal gl;
    CxSystemBuilder builder;
    for(int i=0;i<4;++i)
        builder.OpenPort(i,gl.getIpaddr(i),gl.getPortnumber(i));
}
